
package com.jf.cloud.order.service.impl;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.util.BooleanUtil;
import cn.hutool.core.util.PhoneUtil;
import cn.hutool.core.util.StrUtil;
import com.alibaba.excel.EasyExcel;
import com.alibaba.excel.ExcelWriter;
import com.alibaba.excel.write.builder.ExcelWriterBuilder;
import com.alibaba.excel.write.metadata.WriteSheet;
import com.jf.cloud.api.delivery.dto.DeliveryOrderDTO;
import com.jf.cloud.api.delivery.dto.DeliveryOrderItemDTO;
import com.jf.cloud.api.delivery.feign.DeliveryFeignClient;
import com.jf.cloud.api.delivery.vo.DeliveryCompanyExcelVO;
import com.jf.cloud.api.feign.EsOrderFeignClient;
import com.jf.cloud.api.order.constant.OrderStatus;
import com.jf.cloud.common.constant.Constant;
import com.jf.cloud.common.constant.PayType;
import com.jf.cloud.common.exception.LuckException;
import com.jf.cloud.common.order.bo.EsOrderBO;
import com.jf.cloud.common.order.constant.DeliveryType;
import com.jf.cloud.common.order.constant.OrderActivityType;
import com.jf.cloud.common.order.dto.OrderSearchDTO;
import com.jf.cloud.common.order.util.OrderLangUtil;
import com.jf.cloud.common.order.vo.OrderItemLangVO;
import com.jf.cloud.common.order.vo.OrderItemVO;
import com.jf.cloud.common.order.vo.OrderSelfStationVO;
import com.jf.cloud.common.order.vo.OrderVO;
import com.jf.cloud.common.response.ResponseEnum;
import com.jf.cloud.common.response.ServerResponseEntity;
import com.jf.cloud.common.util.ExcelUtil;
import com.jf.cloud.common.util.PrincipalUtil;
import com.jf.cloud.order.constant.OrderExportError;
import com.jf.cloud.order.constant.RefundStatusEnum;
import com.jf.cloud.order.mapper.OrderMapper;
import com.jf.cloud.order.service.OrderExcelService;
import com.jf.cloud.order.service.OrderItemService;
import com.jf.cloud.order.service.OrderService;
import com.jf.cloud.order.vo.OrderExcelVO;
import com.jf.cloud.order.vo.UnDeliveryOrderExcelVO;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import javax.servlet.http.HttpServletResponse;
import java.math.BigDecimal;
import java.util.*;
import java.util.stream.Collectors;

/**
 * 订单excel
 *
 * @author zz
 * @date 2021/7/20 8:48
 */
@Service
public class OrderExcelServiceImpl implements OrderExcelService {

    @Value("${jfcloud.expose.operation.auth:}")
    private Boolean permission;

    @Autowired
    private OrderMapper orderMapper;
    @Autowired
    private OrderService orderService;
    @Autowired
    private DeliveryFeignClient deliveryFeignClient;
    @Autowired
    private OrderItemService orderItemService;

    @Autowired
    private EsOrderFeignClient esOrderFeignClient;
    @Autowired
    private  OrderSelfStationServiceImpl orderSelfStationService;
    /**
     * 订单类别 0.实物商品 1. 虚拟商品
     */
    public static final String PHYSICAL_ORDER = "实物商品订单";
    public static final String VIRTUAL_ORDER = "虚拟商品订单";

    /**
     * 配送类型
     */
    public final static int DELIVERY_MODE_INDEX = 2;

    /**
     * 快递公司名称
     */
    public final static int DELIVERY_COMPANY_NAME_INDEX = 3;

    private final static int FLOW_ID_MIN = 4;
    private final static int FLOW_ID_MAX = 30;

    public final static String[] DELIVERY_MODE = {DeliveryType.DELIVERY.description(), DeliveryType.NOT_DELIVERY.description()};

    private static final Logger log = LoggerFactory.getLogger(OrderExcelServiceImpl.class);


    @Override
    public List<OrderExcelVO> excelOrderList(OrderSearchDTO orderSearchDTO) {
        // 校验一下店铺名称和供应商名称
        orderService.checkShopName(orderSearchDTO);
        orderService.checkSupplierName(orderSearchDTO);
        List<OrderExcelVO> orderExcelList = orderMapper.excelOrderList(orderSearchDTO);
        List<OrderExcelVO> stationOrderList = orderExcelList.stream().filter(orderExcel -> Objects.nonNull(orderExcel.getDeliveryType()) && DeliveryType.STATION.value().equals(Integer.parseInt(orderExcel.getDeliveryType()))).collect(Collectors.toList());
        List<String> stationOrderIdStrList = stationOrderList.stream().map(OrderExcelVO::getOrderId).collect(Collectors.toList());
        List<Long> stationOrderIds = stationOrderIdStrList.stream().map(Long::valueOf).collect(Collectors.toList());
        // 查找自提订单对应自提人信息
        Map<Long, OrderSelfStationVO> selfStationMap = new HashMap<>(16);
        if (!CollectionUtils.isEmpty(stationOrderIds)) {
            log.info("查询自提订单对应自提人信息，订单id：{}", stationOrderIds);
            List<OrderSelfStationVO> orderSelfStationVOS = orderSelfStationService.listSelfStationByOrderIds(stationOrderIds);
            selfStationMap = orderSelfStationVOS.stream().collect(Collectors.toMap(OrderSelfStationVO::getOrderId, x -> x));
        }
        int index = 0;
        Long orderId = null;
        for (OrderExcelVO orderExcel : orderExcelList) {
            if (!Objects.equals(Long.valueOf(orderExcel.getOrderId()), orderId)) {
                index++;
                orderId = Long.valueOf(orderExcel.getOrderId());
            }
            orderExcel.setSeq(String.valueOf(index));
            // 订单信息
            Integer deliveryType = Integer.valueOf(orderExcel.getDeliveryType());
            orderExcel.setDeliveryType(DeliveryType.getDescription(deliveryType));
            orderExcel.setTotal(conversionPrices(orderExcel.getTotal()));
            orderExcel.setFreightAmount(conversionPrices(orderExcel.getFreightAmount()));
            orderExcel.setReduceAmount(conversionPrices(orderExcel.getReduceAmount()));
            orderExcel.setActualTotal(conversionPrices(orderExcel.getActualTotal()));

            String payType = Objects.isNull(orderExcel.getPayType()) ? Constant.UNPAID : PayType.getPayTypeName(Integer.valueOf(orderExcel.getPayType()));
            orderExcel.setPayType(payType);
            //收货手机号
            if (BooleanUtil.isFalse(permission)) {
                orderExcel.setMobile(PhoneUtil.hideBetween(orderExcel.getMobile()).toString());
            }
            if (Objects.equals(orderSearchDTO.getWaitPurchase(), 1)) {
                orderExcel.setStatus(String.valueOf(OrderStatus.WAIT_PURCHASE.value()));
            }
            if (Objects.equals(deliveryType, DeliveryType.STATION.value())) {
                orderExcel.setStatus(OrderStatus.getStationName(Integer.valueOf(orderExcel.getStatus())));
            } else {
                orderExcel.setStatus(OrderStatus.getDeliveryName(Integer.valueOf(orderExcel.getStatus())));
            }
            String refundStatus = StrUtil.isBlank(orderExcel.getRefundStatus()) ? Constant.NO_AFTER_SALES : RefundStatusEnum.getRefundName(Integer.valueOf(orderExcel.getRefundStatus()));
            orderExcel.setRefundStatus(refundStatus);
            // 订单项信息
            orderExcel.setPrice(conversionPrices(orderExcel.getPrice()));
            orderExcel.setSpuTotalAmount(conversionPrices(orderExcel.getSpuTotalAmount()));
            orderExcel.setShareReduce(conversionPrices(orderExcel.getShareReduce()));
            orderExcel.setActualTotalItem(conversionPrices(orderExcel.getActualTotalItem()));
            BigDecimal platformAmount = new BigDecimal("0.00");
            BigDecimal sellerAmount = new BigDecimal("0.00");
            if (StrUtil.isBlank(orderExcel.getActualTotalItem())) {
                BigDecimal b1 = new BigDecimal(orderExcel.getActualTotalItem());
                //TODO 根据费率调整
                BigDecimal b2 = new BigDecimal("0.056");
                platformAmount = b1.multiply(b2);
                sellerAmount = b1.subtract(platformAmount);
            }
            //平台服务费
            orderExcel.setPlatformAmount(conversionPrices(platformAmount.toString()));
            //结算金额
            orderExcel.setSellerAmount(conversionPrices(sellerAmount.toString()));
            String refundStatusItem = StrUtil.isBlank(orderExcel.getRefundStatusItem()) ? Constant.NO_AFTER_SALES : RefundStatusEnum.getRefundName(Integer.valueOf(orderExcel.getRefundStatusItem()));
            orderExcel.setRefundStatusItem(refundStatusItem);
            orderExcel.setOrderMold(StrUtil.equals(orderExcel.getOrderMold(), "1") ? VIRTUAL_ORDER : PHYSICAL_ORDER);
            // 国际化信息
            OrderItemLangVO orderItemLangVO = OrderLangUtil.handleOrderItemLangVO(orderExcel.getOrderItemLangList());
            if (Objects.nonNull(orderItemLangVO)) {
                orderExcel.setSkuName(orderItemLangVO.getSkuName());
                orderExcel.setSpuName(orderItemLangVO.getSpuName());
            }
            // 补充自提人信息
            OrderSelfStationVO stationVO = selfStationMap.get(Long.valueOf(orderExcel.getOrderId()));
            if (stationVO != null) {
                orderExcel.setConsignee(stationVO.getStationUserName());
                orderExcel.setMobile(stationVO.getStationUserMobile());
                orderExcel.setStationName(stationVO.getStationName()==null?"无":stationVO.getStationName());
            }
        }
        return orderExcelList;
    }

    @Override
    public void excelUnDeliveryOrderList(HttpServletResponse response, OrderSearchDTO orderSearchDTO) {
        List<EsOrderBO> esOrderBOList  = esOrderFeignClient.getUndeliveryOrder(orderSearchDTO);
        log.info("查询未发货订单列表，参数：{}，结果：{}", orderSearchDTO, esOrderBOList);
        List<UnDeliveryOrderExcelVO> unDeliveryOrderList = esOrderBOList.stream().map(esOrderVO -> {
            UnDeliveryOrderExcelVO unDeliveryOrderExcelVO = new UnDeliveryOrderExcelVO();
            unDeliveryOrderExcelVO.setOrderId(esOrderVO.getOrderId().toString());
            unDeliveryOrderExcelVO.setDeliveryType(String.valueOf(esOrderVO.getDeliveryType()));
            unDeliveryOrderExcelVO.setMobile(esOrderVO.getMobile());
            unDeliveryOrderExcelVO.setConsignee(esOrderVO.getConsignee());
            if (Objects.nonNull(esOrderVO.getAddr())) {
                unDeliveryOrderExcelVO.setReceivingAddr(esOrderVO.getProvince() + esOrderVO.getCity() + esOrderVO.getArea() + esOrderVO.getAddr());
            }
            List<OrderItemVO> orderItemVOS = esOrderVO.getOrderItems().stream().map(esOrderItemBO -> {
                OrderItemVO orderItemVO = new OrderItemVO();
                orderItemVO.setOrderItemId(esOrderItemBO.getOrderItemId());
                orderItemVO.setSpuName(esOrderItemBO.getSpuNameZh());
                orderItemVO.setOrderId(esOrderItemBO.getOrderId());
                orderItemVO.setSkuName(esOrderItemBO.getSkuNameZh());
                orderItemVO.setCount(esOrderItemBO.getCount());
                orderItemVO.setGiveawayAmount(esOrderItemBO.getGiveawayAmount());
                orderItemVO.setActivityId(esOrderItemBO.getActivityId());
                orderItemVO.setActivityType(esOrderItemBO.getActivityType());
                orderItemVO.setBeDeliveredNum(esOrderItemBO.getBeDeliveredNum());
                return orderItemVO;
            }).collect(Collectors.toList());
            unDeliveryOrderExcelVO.setOrderItemList(orderItemVOS);
            return unDeliveryOrderExcelVO;
        }).collect(Collectors.toList());


        List<UnDeliveryOrderExcelVO> excelVOList = new ArrayList<>();
        Map<Integer, String[]> map = new HashMap<>(8);
        map.put(DELIVERY_MODE_INDEX, DELIVERY_MODE);
        int index = 0;
        //合并单元格
        Long orderId = null;
        for (UnDeliveryOrderExcelVO orderExcel : unDeliveryOrderList) {
            List<OrderItemVO> orderItemList = orderExcel.getOrderItemList();
            if (CollectionUtil.isEmpty(orderItemList)) {
                continue;
            }
            if (!Objects.equals(Long.valueOf(orderExcel.getOrderId()), orderId)) {
                index++;
                orderId = Long.valueOf(orderExcel.getOrderId());
            }
            orderExcel.setSeq(String.valueOf(index));
            //物流信息
            orderExcel.setDeliveryType(DeliveryType.getDescription(Integer.valueOf(orderExcel.getDeliveryType())));
            //快递公司名称
            map.put(DELIVERY_COMPANY_NAME_INDEX, getDeliveryCompanyName());

            handleSpuExcelInfo(excelVOList, orderItemList, orderExcel);
        }
        writeExcel(response, excelVOList, map);
    }

    private void handleSpuExcelInfo(List<UnDeliveryOrderExcelVO> excelVOList, List<OrderItemVO> orderItemVOList, UnDeliveryOrderExcelVO orderExcel) {
        Map<Long, List<OrderItemVO>> map = orderItemVOList.stream().filter(orderItemVO -> Objects.equals(orderItemVO.getActivityType(), OrderActivityType.GIVEAWAY.value()))
                .collect(Collectors.groupingBy(OrderItemVO::getActivityId));

        for (OrderItemVO orderItemVO : orderItemVOList) {
            // 赠品订单项不需要进行处理
            if (Objects.equals(orderItemVO.getActivityType(), OrderActivityType.GIVEAWAY.value())) {
                continue;
            }
            int num = 0;
            if (map.containsKey(orderItemVO.getOrderItemId())) {
                for (OrderItemVO giveawayVO : map.get(orderItemVO.getOrderItemId())) {
                    num++;
                    UnDeliveryOrderExcelVO unOrderExcel = new UnDeliveryOrderExcelVO();
                    unOrderExcel = addNewExcelVo(unOrderExcel, orderItemVO, orderExcel);
                    unOrderExcel.setGiveawayName(giveawayVO.getSpuName() + "  " + giveawayVO.getSkuName());
                    unOrderExcel.setGiveawayCount(giveawayVO.getChangeNum());
                    if (num > 1) {
                        unOrderExcel.setSpuName(null);
                        unOrderExcel.setSkuName(null);
                        unOrderExcel.setCount(null);
                    }
                    excelVOList.add(unOrderExcel);
                }
            } else {
                UnDeliveryOrderExcelVO unOrderExcel = new UnDeliveryOrderExcelVO();
                unOrderExcel = addNewExcelVo(unOrderExcel, orderItemVO, orderExcel);
                excelVOList.add(unOrderExcel);
            }
        }
    }

    private UnDeliveryOrderExcelVO addNewExcelVo(UnDeliveryOrderExcelVO unOrderExcel, OrderItemVO orderItemVO, UnDeliveryOrderExcelVO orderExcel) {
        unOrderExcel.setOrderId(orderExcel.getOrderId());
        unOrderExcel.setSeq(orderExcel.getSeq());
        unOrderExcel.setDeliveryType(orderExcel.getDeliveryType());
        unOrderExcel.setDeliveryCompanyName(orderExcel.getDeliveryCompanyName());
        unOrderExcel.setDeliveryNo(orderExcel.getDeliveryNo());
        unOrderExcel.setConsignee(orderExcel.getConsignee());
        unOrderExcel.setMobile(orderExcel.getMobile());
        unOrderExcel.setReceivingAddr(orderExcel.getReceivingAddr());
        unOrderExcel.setSpuName(orderItemVO.getSpuName());
        unOrderExcel.setSkuName(orderItemVO.getSkuName());
        unOrderExcel.setCount(orderItemVO.getBeDeliveredNum());
        return unOrderExcel;
    }


    @Override
    public void exportOrderExcel(List<UnDeliveryOrderExcelVO> list, Map<Integer, List<String>> errorMap) {
        List<DeliveryCompanyExcelVO> companyExcelList = deliveryFeignClient.listDeliveryCompany().getData();
        Map<String, Long> companyNameMap = companyExcelList.stream().collect(Collectors.toMap(DeliveryCompanyExcelVO::getName, DeliveryCompanyExcelVO::getDeliveryCompanyId));
        Map<String, List<UnDeliveryOrderExcelVO>> orderMap = list.stream().collect(Collectors.groupingBy(UnDeliveryOrderExcelVO::getSeq));
        for (String seq : orderMap.keySet()) {
            List<UnDeliveryOrderExcelVO> orderExcels = orderMap.get(seq);
            DeliveryOrderDTO deliveryOrder = new DeliveryOrderDTO();
            try {
                loadOrderData(orderExcels, deliveryOrder, companyNameMap, errorMap);
            } catch (Exception e) {
                addOrderExcelError(errorMap, OrderExportError.OTHER, e.getMessage());
                continue;
            }
            if (Objects.nonNull(deliveryOrder.getSelectOrderItems())) {
                try {
                    //订单发货
                    orderService.delivery(deliveryOrder);
                } catch (Exception e) {
                    addOrderExcelError(errorMap, OrderExportError.ORDER_ID, seq);
                }
            }
        }
    }

    @Override
    public String orderExportError(Map<Integer, List<String>> errorMap) {
        StringBuilder info = new StringBuilder();
        for (Integer key : errorMap.keySet()) {
            List<String> list = errorMap.get(key);
            OrderExportError orderExportError = OrderExportError.instance(key);
            if (Objects.equals(orderExportError, OrderExportError.OTHER)) {
                info.append(list.toString() + "\n");
                continue;
            }
            info.append(orderExportError.errorInfo() + list.toString() + "\n");
        }
        return info.toString();
    }

    private void loadOrderData(List<UnDeliveryOrderExcelVO> orderExcels, DeliveryOrderDTO deliveryOrder, Map<String, Long> companyNameMap, Map<Integer, List<String>> errorMap) {
        UnDeliveryOrderExcelVO orderExcel = orderExcels.get(0);
        //订单id不能为空且该订单必须为当前店铺的订单，并为待发货状态
        if (Objects.isNull(orderExcel.getOrderId())) {
            addOrderExcelError(errorMap, OrderExportError.ORDER_ID, orderExcel.getSeq());
            return;
        }
        OrderVO order = orderService.getOrderByOrderId(Long.valueOf(orderExcel.getOrderId()));
        if (Objects.isNull(order) || !Objects.equals(order.getStatus(), OrderStatus.PAYED.value())) {
            addOrderExcelError(errorMap, OrderExportError.ORDER_ID, orderExcel.getSeq());
            return;
        }
        deliveryOrder.setOrderAddrId(order.getOrderAddrId());
        List<OrderItemVO> orderItemVOList = orderItemService.listOrderItemAndLangByOrderId(Long.valueOf(orderExcel.getOrderId()));
        for (OrderItemVO orderItemVO : orderItemVOList) {
            if (Objects.equals(orderItemVO.getBeDeliveredNum(), 0) || !Objects.equals(orderItemVO.getBeDeliveredNum(), orderItemVO.getCount())) {
                //如果该订单已经被部分发货或全部发货则不予发货
                addOrderExcelError(errorMap, OrderExportError.BE_DELIVERY, orderExcel.getSeq());
                return;
            }
        }
        deliveryOrder.setOrderId(Long.valueOf(orderExcel.getOrderId()));
        //配送方式不为空
        if (Objects.isNull(orderExcel.getDeliveryType())) {
            addOrderExcelError(errorMap, OrderExportError.DELIVERY_TYPE, orderExcel.getSeq());
            return;
        }
        if (Objects.equals(orderExcel.getDeliveryType(), DeliveryType.DELIVERY.description())) {
            deliveryOrder.setDeliveryType(DeliveryType.DELIVERY.value());
        } else if (Objects.equals(orderExcel.getDeliveryType(), DeliveryType.NOT_DELIVERY.description())) {
            deliveryOrder.setDeliveryType(DeliveryType.NOT_DELIVERY.value());
        }
        //订单的配送方式为快递配送时，导入的配送方式可选择快递配送和无需快递，其他情况订单的配送方式与导入的配送方式需保持一致
        if (!Objects.equals(deliveryOrder.getDeliveryType(), order.getDeliveryType()) && !Objects.equals(order.getDeliveryType(), DeliveryType.DELIVERY.value())) {
            addOrderExcelError(errorMap, OrderExportError.DELIVERY_TYPE, orderExcel.getSeq());
            return;
        } else if (!Objects.equals(deliveryOrder.getDeliveryType(), order.getDeliveryType()) && Objects.equals(order.getDeliveryType(), DeliveryType.DELIVERY.value())) {
            if (!Objects.equals(deliveryOrder.getDeliveryType(), DeliveryType.NOT_DELIVERY.value())) {
                addOrderExcelError(errorMap, OrderExportError.DELIVERY_TYPE, orderExcel.getSeq());
                return;
            }
        }

        //只有快递才需要快递校验
        if (Objects.equals(orderExcel.getDeliveryType(), DeliveryType.DELIVERY.description())) {
            //快递公司校验
            if (Objects.isNull(orderExcel.getDeliveryCompanyName())) {
                addOrderExcelError(errorMap, OrderExportError.DELIVERY_COMPANY_NAME, orderExcel.getSeq());
                return;
            }
            deliveryOrder.setDeliveryCompanyId(companyNameMap.get(orderExcel.getDeliveryCompanyName()));
            //快递单号校验
            String deliveryNo = orderExcel.getDeliveryNo();
            deliveryNo = StringUtils.deleteWhitespace(deliveryNo);
            boolean isTrue = StrUtil.isBlank(deliveryNo) ||
                    (deliveryNo.length() > FLOW_ID_MAX || deliveryNo.length() < FLOW_ID_MIN)
                    || !PrincipalUtil.isMatching(PrincipalUtil.WITHOUT_CHINESE, deliveryNo);
            if (isTrue) {
                addOrderExcelError(errorMap, OrderExportError.DELIVERY_NO, orderExcel.getSeq());
                return;
            }
            deliveryOrder.setDeliveryNo(deliveryNo);
        }
        List<DeliveryOrderItemDTO> orderItemFeignVOList = new ArrayList<>();
        for (OrderItemVO orderItemVO : orderItemVOList) {
            DeliveryOrderItemDTO deliveryOrderItemFeignVO = new DeliveryOrderItemDTO();
            deliveryOrderItemFeignVO.setOrderItemId(orderItemVO.getOrderItemId());
            deliveryOrderItemFeignVO.setChangeNum(orderItemVO.getBeDeliveredNum());
            deliveryOrderItemFeignVO.setPic(orderItemVO.getPic());
            deliveryOrderItemFeignVO.setSpuName(orderItemVO.getSpuName());
            orderItemFeignVOList.add(deliveryOrderItemFeignVO);
        }
        deliveryOrder.setSelectOrderItems(orderItemFeignVOList);
    }

    private void addOrderExcelError(Map<Integer, List<String>> errorMap, OrderExportError orderExportError, String error) {
        List<String> list = errorMap.get(orderExportError.value());
        if (CollUtil.isEmpty(list)) {
            list = new ArrayList<>();
            errorMap.put(orderExportError.value(), list);
        }
        list.add(error);
    }

    /**
     * 转换金额格式
     *
     * @param num
     * @return
     */
    private String conversionPrices(String num) {
        if (StrUtil.isBlank(num)) {
            return num;
        }
        BigDecimal b1 = new BigDecimal(num);
        BigDecimal b2 = new BigDecimal(Constant.PRICE_MAGNIFICATION);
        double price = b1.divide(b2, 2, BigDecimal.ROUND_HALF_UP).doubleValue();
        return Double.toString(price);
    }

    private String[] getDeliveryCompanyName() {
        //快递公司名称
        ServerResponseEntity<List<DeliveryCompanyExcelVO>> companyData = deliveryFeignClient.listDeliveryCompany();
        if (!Objects.equals(companyData.getCode(), ResponseEnum.OK.value())) {
            throw new LuckException(companyData.getMsg());
        }
        List<DeliveryCompanyExcelVO> companyExcelList = companyData.getData();
        if (CollUtil.isNotEmpty(companyExcelList)) {
            String[] companyNames = new String[companyExcelList.size()];
            for (int i = 0; i < companyExcelList.size(); i++) {
                DeliveryCompanyExcelVO companyExcel = companyExcelList.get(i);
                companyNames[i] = companyExcel.getName();
            }
            return companyNames;
        }
        return null;
    }

    private void writeExcel(HttpServletResponse response, List<UnDeliveryOrderExcelVO> list, Map<Integer, String[]> map) {
        ExcelWriter excelWriter = null;
        try {
//            ExcelUtil.soleExcel(response,list, UnDeliveryOrderExcelVO.EXCEL_NAME, UnDeliveryOrderExcelVO.MERGE_ROW_INDEX, UnDeliveryOrderExcelVO.MERGE_COLUMN_INDEX, UnDeliveryOrderExcelVO.class);

            ExcelWriterBuilder excelWriterMerge = ExcelUtil.getExcelWriterMerge(response, UnDeliveryOrderExcelVO.EXCEL_NAME, UnDeliveryOrderExcelVO.MERGE_ROW_INDEX,
                    UnDeliveryOrderExcelVO.MERGE_COLUMN_INDEX);
            excelWriter = ExcelUtil.getExcelModel(excelWriterMerge, map, Constant.START_ROW).build();
            //订单信息
            //写入
            WriteSheet writeSheet = EasyExcel.writerSheet(UnDeliveryOrderExcelVO.EXCEL_NAME).head(UnDeliveryOrderExcelVO.class).build();
            excelWriter.write(list, writeSheet);
        } catch (Exception e) {
            log.error("导出excel失败", e);
        } finally {
            if (excelWriter != null) {
                excelWriter.finish();
            }
        }
    }
}
